package com.guli.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,String type, Integer id) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if(type.equals("base")){
            wrapper.and(w->w.eq("attr_type",1).or().eq("attr_type",2));
        }else {
            wrapper.and(w->w.eq("attr_type",0).or().eq("attr_type",2));
        }
        if (id!=0){
            wrapper.eq("catelog_id",id);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void updById(AttrEntity attr) {
        if (attr.getCatelogId()!=null){
            CategoryEntity categoryEntity = categoryDao.selectById(attr.getCatelogId());
            attr.setCatelogName(categoryEntity.getName());
        }
        if(attr.getAttrGroupId()!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr.getAttrGroupId());
            attr.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }
        baseMapper.updateById(attr);
    }

    @Override
    public void add(AttrEntity attr) {
        if (attr.getCatelogId()!=null){
            CategoryEntity categoryEntity = categoryDao.selectById(attr.getCatelogId());
            attr.setCatelogName(categoryEntity.getName());
        }
        if(attr.getAttrGroupId()!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr.getAttrGroupId());
            attr.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }
        baseMapper.insert(attr);
    }

    @Override
    public AttrEntity getByAid(Long attrId) {
        List<Long> list = new ArrayList<>();
        AttrEntity attrEntity = baseMapper.selectById(attrId);
        getAttrGroup(list,attrEntity.getCatelogId());
        Collections.reverse(list);
        attrEntity.setPaths(list.toArray(new Long[list.size()]));
        return attrEntity;
    }

    @Override
    public List<AttrEntity> attrRelation(Integer attrGroupId) {
        List<AttrEntity> attrEntities = baseMapper.selectList(new QueryWrapper<AttrEntity>().eq("attr_group_id", attrGroupId).eq("attr_type",1));
        return attrEntities;
    }

    @Override
    public PageUtils noattrRelation(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>().eq("attr_type",1).isNull("attr_group_id")
        );
        return new PageUtils(page);
    }

    @Override
    public void relationUpdate(AttrEntity attrEntities) {
        attrDao.relationUpdate(attrEntities);
    }

    private void getAttrGroup(List<Long> list, Long catId){
        list.add(catId);
        CategoryEntity categoryEntity = categoryDao.selectById(catId);
        if (!categoryEntity.getParentCid().equals(0L)){
            getAttrGroup(list,categoryEntity.getParentCid());
        }
    }

}